#include"socket.hpp"
#include<pthread.h>
#include<unistd.h>
using namespace std;
class httpserver;
class threaddata
{

public:
threaddata(int sockfd,httpserver*s)
: sockfd_(sockfd)
,ser(s)
{

}



public:
int sockfd_;
httpserver*ser;


};
class httpserver
{ 
 public:
httpserver(uint16_t port)
:port_(port)
{



}
void start()
{

sock_.Socket();
sock_.Bind(port_);
sock_.Listen();

while(1)
{

uint16_t clientport;
string clientip;
int newfd=sock_.Accept(&clientport,&clientip);
if(newfd<0)
{
  cout<<"accept fail..."<<endl;
  continue;
}

pthread_t pid;
threaddata*st=new threaddata(newfd,this);
pthread_create(&pid,nullptr,fun,st);



}



}
static void* fun(void*argv)
{
pthread_detach(pthread_self());


threaddata*st=static_cast<threaddata*>(argv);
char buffer[10240];
int n=read(st->sockfd_,buffer,sizeof(buffer)-1);
if(n>0)
 {
    buffer[n]=0;
    cout<<buffer<<endl;

 }
 return nullptr;

}


~httpserver()
{


    
}




private:
sock sock_;
uint16_t port_;





};